package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AnalysisItemsMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AnalysisItemsResultMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AnalysisMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AnalysisResultMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Analysis;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.AnalysisItems;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.AnalysisItemsResult;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.AnalysisResult;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by yangcs on 2017/6/8.
 */
public class AnalysisServiceImpl implements AnalysisService {

    @Autowired
    private AnalysisMapper analysisMapper;

    @Autowired
    private AnalysisItemsMapper analysisItemsMapper;

    @Autowired
    private AnalysisResultMapper analysisResultMapper;

    @Autowired
    private AnalysisItemsResultMapper analysisItemsResultMapper;

    @Override
    public int saveAnalysis(Analysis analysis) {
        analysis.setAnalysisId(UniqueUtil.uuid());
        int result = analysisMapper.insert(analysis);
        if( result > 0 ){
            List<AnalysisItems> items = JSONObject.parseArray(analysis.getAnalysisItems(), AnalysisItems.class);
            if( items != null && items.size() > 0 ){
               for( AnalysisItems item:items ){
                   item.setAnalysisItemsId(UniqueUtil.uuid());
                   item.setAnalysisId(analysis.getAnalysisId());
                   result = analysisItemsMapper.insert(item);
                   if( result < 1 ){
                       break;
                   }
               }
            }
        }
        return result;
    }

    @Override
    public JSONObject list(Analysis analysis) {
        JSONObject jsonObject = new JSONObject();
        analysis.setOffset(analysis.getOffset() == null ? 0 : analysis.getOffset());
        analysis.setLimit(analysis.getLimit() == null ? 10 : analysis.getLimit());
        jsonObject.put("total", analysisMapper.queryCount(analysis));
        jsonObject.put("rows", JSON.toJSON(analysisMapper.queryList(analysis)));
        return jsonObject;
    }

    @Override
    public int deleteById(String analysisId) {
        return analysisMapper.delbyId(analysisId);
    }

    @Override
    public JSONObject listData(Analysis analysis) {
        JSONObject jsonObject = new JSONObject();
        analysis.setLimit(analysis.getLimit() == null ? 10 : analysis.getLimit());
        analysis.setOffset(analysis.getOffset() == null ? 0 : analysis.getOffset());
        jsonObject.put("total", analysisResultMapper.queryCount(analysis));
        jsonObject.put("rows", JSON.toJSON(analysisResultMapper.queryList(analysis)));
        return jsonObject;
    }

    @Override
    public int addResult(String analysisId,String itemsResult,String userId) {
        Date date = new Date();
        List<AnalysisItemsResult> items;
        int result = 0;
        List<AnalysisResult> results =  JSONObject.parseArray(itemsResult, AnalysisResult.class);
        if( results != null && results.size() > 0 ){
            for( AnalysisResult analysisResult:results ){
                analysisResult.setAnalysisDate(date);
                analysisResult.setReportUserId(userId);
                analysisResult.setAnalysisId(analysisId);
                analysisResult.setAnalysisResultId(UniqueUtil.uuid());
                result = analysisResultMapper.insert(analysisResult);
                if( result > 0 ){
                    items = JSONObject.parseArray(analysisResult.getItemsResult(), AnalysisItemsResult.class);
                    if( items != null && items.size() > 0 ){
                        for( AnalysisItemsResult item:items ){
                            item.setReportUserId(analysisResult.getReportUserId());
                            item.setAnalysisResultId(analysisResult.getAnalysisResultId());
                            item.setAnalysisDate(date);
                            result = analysisItemsResultMapper.insert(item);
                            if( result < 1 ){
                                break;
                            }
                        }
                    }
                }
            }
        }
        return result;
    }

    @Override
    public AnalysisResult queryAnalysisResult(AnalysisResult analysisResult) {
        return analysisResultMapper.queryAnalysisResult(analysisResult);
    }

    @Override
    public List<AnalysisResult> queryAnalysisResultList(AnalysisResult analysisResult) {
        List<AnalysisResult> list = analysisResultMapper.queryAnalysisResultList(analysisResult);
        List<AnalysisResult> newList = new ArrayList<>();
        if( list != null && list.size() > 0 ){
            for ( AnalysisResult result:list ){
                analysisResult.setIsNeed(result.getIsNeed());
                analysisResult.setAnalysisResultId(result.getAnalysisResultId());
                result.setItems( analysisItemsResultMapper.queryAnalysisItemsResult(analysisResult) );
                newList.add(result);
            }
        }
        return newList;
    }

    @Override
    public List<AnalysisItemsResult> queryAnalysisItemsResult(AnalysisResult analysisResult) {
        return analysisItemsResultMapper.queryAnalysisItemsResult(analysisResult);
    }

    @Override
    public List<AnalysisItems> queryItems(AnalysisResult analysisResult) {
        return analysisItemsMapper.queryItems(analysisResult);
    }

    @Override
    public List<AnalysisItemsResult> queryNeeds() {
        return analysisItemsResultMapper.queryNeeds();
    }

    @Override
    public JSONObject checkListData(Analysis analysis) {
        JSONObject jsonObject = new JSONObject();
        analysis.setLimit(analysis.getLimit() == null ? 10 : analysis.getLimit());
        analysis.setOffset(analysis.getOffset() == null ? 0 : analysis.getOffset());
        jsonObject.put("total", analysisResultMapper.queryCheckCount(analysis));
        jsonObject.put("rows", JSON.toJSON(analysisResultMapper.queryCheckList(analysis)));
        return jsonObject;
    }

    @Override
    public int check(String analysisId, String analysisDate, int analysisStatus, String applyUser) {
        Map map = new HashMap<>();
        map.put("analysisId",analysisId);
        map.put("analysisDate",analysisDate);
        map.put("analysisStatus",analysisStatus);
        map.put("applyUser",applyUser);
        return analysisMapper.insertCheck(map);
    }
}
